package cn.wanho.service.Impl;

import cn.wanho.dto.ExamPaper;
import cn.wanho.mapper.ExamPaperMapper;
import cn.wanho.rvo.*;
import cn.wanho.service.exam.ExamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author jingxu
* @date 2022-8-5
*/
@Service
public class ExamServiceImpl implements ExamService {


    @Resource
    private ExamPaperMapper examPaperMapper;


    /**
     * @param paperId
     * @author jingxu
     * @date 2022-8-5
     * 学生参加考试
     */
    @Override
    public ResultVO takeExamination(Integer paperId) {
        //先通过传入当前要考试的试卷id来查询这场考试的考试时间和结束时间
        ExamPaper paper = examPaperMapper.takeExamPaper(paperId);
        Date current = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //拿到试卷
        if (paper == null){


        }
        if (paper != null) {
            Date startTime = paper.getStartTime();
            Date endTime = paper.getEndTime();

            String sTime = format.format(startTime);
            String eTime = format.format(endTime);
            String c = format.format(current);


            Date cTime = null;
            Date ssTime = null;
            Date eeTime = null;
            try {
                //当前系统时间
                cTime = format.parse(c);

                //考试开始时间
                ssTime = format.parse(sTime);

                //考试结束时间
                eeTime = format.parse(eTime);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            //表示还未到考试的考试时间
            assert cTime != null;
            if ((cTime.compareTo(ssTime)) < 0) {
                return ResultVO.faiure(ResultEnum.EXAM_TIME_NO_ARRIVE.getResCode(), ResultEnum.EXAM_TIME_NO_ARRIVE.getResMsg());
            }
            //表示当前系统时间已到达考试时间
            if ((cTime.compareTo(ssTime) > 0 ) & (cTime.compareTo(eeTime) < 0)) {
                //进入考试，根据传入的试卷id去拿试卷的内容，将试卷的内容返回给前端，让学生去答题

                //当前考试试卷上所有的选择题
                List<ExamChoose> chooses = examPaperMapper.selectChoose(paperId);
                List<ExamSubject> blanks = examPaperMapper.selectBlanks(paperId);
                List<ExamSubject> tF = examPaperMapper.selectTf(paperId);
                List<ExamSubject> big = examPaperMapper.selectBig(paperId);

                //将各题型进行序号的排序
                Collections.sort(chooses);
                Collections.sort(blanks);
                Collections.sort(tF);
                Collections.sort(big);



                ArrayList<Object> list = new ArrayList<>();
                list.add("选择题:");
                list.add(chooses);
                list.add("填空题:");
                list.add(blanks);
                list.add("判断题:");
                list.add(tF);
                list.add("简答题:");
                list.add(big);

                return ResultVO.success(ResultEnum.EXAM_START_NOW.getResCode(), ResultEnum.EXAM_START_NOW.getResMsg(), list);
            }

            if ((eeTime.compareTo(cTime) < 0)) {
                //表示考试时间过了
                return ResultVO.faiure(ResultEnum.EXAM_TIME_FINISH.getResCode(), ResultEnum.EXAM_TIME_FINISH.getResMsg());
            }
        }
            return ResultVO.faiure(ResultEnum.PAPER_NO_EXISTS.getResCode(), ResultEnum.PAPER_NO_EXISTS.getResMsg());
    }

    /**
     * @author jingxu
     * @date 2022-8-7
     * 学生查询成绩
     */
    @Override
    public ResultVO queryExamContentScore(Integer paperId, Integer studentId) {
        List<ExamChooseVO> chooseVOS = examPaperMapper.selectExamChoose(studentId, paperId);
        List<ExamSubjectVO> blanksVOs = examPaperMapper.selectExamBlanks(studentId, paperId);
        List<ExamSubjectVO> tfVOs = examPaperMapper.selectExamTf(studentId, paperId);
        List<ExamSubjectVO> bigVOs = examPaperMapper.selectExamBig(studentId, paperId);

        Collections.sort(chooseVOS);
        Collections.sort(blanksVOs);
        Collections.sort(tfVOs);
        Collections.sort(bigVOs);

        ArrayList<Object> list = new ArrayList<>();
        list.add("选择题");
        list.add(chooseVOS);
        list.add("填空题");
        list.add(blanksVOs);
        list.add("判断题");
        list.add(tfVOs);
        list.add("简答题");
        list.add(bigVOs);

        if ((chooseVOS.isEmpty()) &&  (blanksVOs.isEmpty()) && (tfVOs.isEmpty() )&& (bigVOs.isEmpty())){
            return ResultVO.faiure(ResultEnum.QUERY_STUDENT_SCORE_FAILURE.getResCode(),ResultEnum.QUERY_STUDENT_SCORE_FAILURE.getResMsg());
        }else {
            return ResultVO.success(ResultEnum.QUERY_STUDENT_SCORE_SUCCESS.getResCode(),ResultEnum.QUERY_STUDENT_SCORE_SUCCESS.getResMsg(),list);
        }
    }
}
